package presentation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import control.CControl;
import entity.EPatternRegister;

public class PNodePanel extends JPanel implements Observer, ComponentListener {
	private static final long serialVersionUID = 1L;
	private JLabel nodeNameLabel, ODversionLabel, statusLabel, errorLabel;
	private PRegisterValue ODversionValue;
	private PRegisterText statusValue, errorValue;
	private int nodeId;
	private Font font = new Font("Arial",Font.BOLD,16);
	private String nodeName;
	
	public PNodePanel(String nodeName, int nodeId, CControl cControl) {
		this.nodeId = nodeId;
		this.nodeName = nodeName;
		this.setBackground(Color.lightGray);
		this.addComponentListener(this);
		((FlowLayout) this.getLayout()).setVgap(0);
		this.setBorder(BorderFactory.createLineBorder(Color.lightGray,1));
		
		nodeNameLabel = new JLabel(nodeName,JLabel.CENTER);
		nodeNameLabel.setFont(new Font("Arial", Font.BOLD, 20));
		
		ODversionLabel = new JLabel("OD version:");
		ODversionLabel.setFont(font);
		statusLabel = new JLabel("Status:");
		statusLabel.setFont(font);
		errorLabel = new JLabel("Error:");
		errorLabel.setFont(font);
		
		ODversionValue = new PRegisterValue("-",JLabel.RIGHT);
		statusValue = new PRegisterText("-",JLabel.RIGHT);
		errorValue = new PRegisterText("-",JLabel.RIGHT);
		
		cControl.addRegisterObserver(cControl.getRegisterName(nodeName, nodeName + "_EPLREG"), ODversionValue);
		cControl.addRegisterObserver(cControl.getRegisterName(nodeName, nodeName + "_StatusReg"), statusValue);
		cControl.addRegisterObserver(cControl.getRegisterName(nodeName, nodeName + "_ErrorReg"), errorValue);
		
		this.add(nodeNameLabel);
		this.add(ODversionLabel);
		this.add(ODversionValue);
		this.add(statusLabel);
		this.add(statusValue);
		this.add(errorLabel);
		this.add(errorValue);
	}
	
	public void setActive(boolean active) {
		if (active) {
			this.setBackground(new Color(219,255,183));
		} else {
			this.setBackground(new Color(255,130,130));
		}
	}

	@Override
	public void update(Observable obs, Object arg) {
		if (obs instanceof EPatternRegister) {
				//boolean active = (Long.parseLong((String) arg,2) & (0x01 << nodeId)) == (0x01 << nodeId);
				if((nodeName.equalsIgnoreCase("MBN")) )
				{
					System.out.println(((Long.parseLong((String) arg,2) & 0x20) == 0x20));
					setActive(true);
				}
				else if((nodeName.equalsIgnoreCase("DBN")) && ((Long.parseLong((String) arg,2) & 0x02) == 0x02))
				{
					setActive(true);
				}
				else if((nodeName.equalsIgnoreCase("DLN")) && ((Long.parseLong((String) arg,2) & 0x01) == 0x01))
				{
					setActive(true);
				}
				else if((nodeName.equalsIgnoreCase("FSN")) && ((Long.parseLong((String) arg,2) & 0x08) == 0x08))
				{
					setActive(true);
				}
				else if((nodeName.equalsIgnoreCase("RSN")) && ((Long.parseLong((String) arg,2) & 0x10) == 0x10))
				{
					setActive(true);
				}
				else if((nodeName.equalsIgnoreCase("LPN")) && ((Long.parseLong((String) arg,2) & 0x4) == 0x4))
				{
					setActive(true);
				}
				else if(nodeName.equalsIgnoreCase("MN"))
				{
					setActive(true);
				}
				else
				{
					setActive(false);
				}
		}
	}

	@Override
	public void componentHidden(ComponentEvent arg0) {}

	@Override
	public void componentMoved(ComponentEvent arg0) {}

	@Override
	public void componentResized(ComponentEvent arg0) {
		Double headerHeight = this.getHeight()*0.31;
		Double labelHeight = this.getHeight()*0.23;
		
		nodeNameLabel.setPreferredSize(new Dimension(this.getWidth(),headerHeight.intValue()));
		ODversionLabel.setPreferredSize(new Dimension(120,labelHeight.intValue()));
		ODversionValue.setPreferredSize(new Dimension(this.getWidth()-140,labelHeight.intValue()));
		statusLabel.setPreferredSize(new Dimension(120,labelHeight.intValue()));
		statusValue.setPreferredSize(new Dimension(this.getWidth()-140,labelHeight.intValue()));
		errorLabel.setPreferredSize(new Dimension(120,labelHeight.intValue()));
		errorValue.setPreferredSize(new Dimension(this.getWidth()-140,labelHeight.intValue()));
		
		updateUI();
	}

	@Override
	public void componentShown(ComponentEvent arg0) {}
}
